<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/26
 * Time: 14:10
 */
namespace Admin\Controller;
use Think\Controller;
class FinancialStatementsController extends AdminController{
    /*用户资金*/
    public function index(){
        //读取配置
        $s_type = C('MONEY_LOG_TYPE');
        $s_types = array_flip($s_type);
        $this->assign('s_types',$s_types);

        $model = M('futures_fund_record');
            //状态搜索
        if($_REQUEST['status']){
            if($_REQUEST['status']==1){
                $map['type']=array('eq',1);//数据库字段
                $status=1;
            }elseif($_REQUEST['status']==2){
                $map['type']=array('eq',2);
                $status=2;
            }elseif($_REQUEST['status']==3){
                $map['type']=array('eq',3);
                $status=3;
            }elseif($_REQUEST['status']==4){
                $map['type']=array('eq',4);
                $status=4;
            }elseif($_REQUEST['status']==5){
                $map['type']=array('eq',5);
                $status=5;
            }elseif($_REQUEST['status']==6){
                $map['type']=array('eq',6);
                $status=6;
            }elseif($_REQUEST['status']==7){
                $map['type']=array('eq',7);
                $status=7;
            }elseif($_REQUEST['status']==8){
                $map['type']=array('eq',8);
                $status=8;
            }else{
                 $status=-2;
            }
        }
        else{
             $status=-2;
       }
       $this->assign('status',$status);
        //用户名搜索
         if($_REQUEST['nickname']){
            $nickname=$_REQUEST['nickname'];
            $map['nickname']=array('like',"%".trim($nickname)."%");
            $this->assign('nickname',$nickname,'','trim');
         }

         //时间搜索
         if ($_REQUEST['create_time']) {//交易时间
            $time=strtotime($_REQUEST['create_time']);
            $start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
            $end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $map['create_time'] = array('between',"$start_time,$end_time");//开始到结束之间的所有内容
            $this->assign('create_time',$_REQUEST['create_time']);
            
        }

        $count_list=$model->alias('i')->field('i.*,p.nickname')->join('__FOLLOW__ p ON p.id = i.user_id')->where($map)->count();//查询总共有多少条数据
       // print_r($count_list);exit;
        //$count=count($count_list);//赋值给
        $count=$count_list;//赋值给
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(15)
        $show       = $Page->show();// 分页显示输出
        $list = $model->alias('i')->field('i.*,p.nickname')->join('__FOLLOW__ p ON p.id = i.user_id')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    /**
     * 导出
     */

    public function download(){
        $map=array();
                //读取配置
        $s_type = C('MONEY_LOG_TYPE');
        $s_types = array_flip($s_type);
        $this->assign('s_types',$s_types);

            //状态搜索
        if($_REQUEST['status']){
            if($_REQUEST['status']==1){
                $map['type']=array('eq',1);//数据库字段
                $status=1;
            }elseif($_REQUEST['status']==2){
                $map['type']=array('eq',2);
                $status=2;
            }elseif($_REQUEST['status']==3){
                $map['type']=array('eq',3);
                $status=3;
            }elseif($_REQUEST['status']==4){
                $map['type']=array('eq',4);
                $status=4;
            }elseif($_REQUEST['status']==5){
                $map['type']=array('eq',5);
                $status=5;
            }elseif($_REQUEST['status']==6){
                $map['type']=array('eq',6);
                $status=6;
            }elseif($_REQUEST['status']==7){
                $map['type']=array('eq',7);
                $status=7;
            }elseif($_REQUEST['status']==8){
                $map['type']=array('eq',8);
                $status=8;
            }else{
                 $status=-2;
            }
        }
        else{
             $status=-2;
       }
       $this->assign('status',$status);

         //时间搜索
         if ($_REQUEST['create_time']) {//交易时间
            $time=strtotime($_REQUEST['create_time']);
            $start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
            $end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $map['create_time'] = array('between',"$start_time,$end_time");//开始到结束之间的所有内容
        }
        $this->assign('create_time',$_REQUEST['create_time']); 

         //用户名搜索
         if($_REQUEST['nickname']){
            $nickname=$_REQUEST['nickname'];
            $follow_name['nickname']=array('like',"%".trim($nickname)."%");
            $user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
            foreach($user_list as $key=>$val){
                $uid.=$val['id'].',';
            }
            $map['user_id']=array('in',trim($uid,','));
            $this->assign('nickname',$nickname,'','trim');
         }

        $list=M('futures_fund_record')->where($map)->order('create_time desc')->select();
        foreach($list as $k=>$v){
            $where_follow['id']=array('eq',$v['user_id']);//用户名称
            $user_name=M('follow')->where($where_follow)->getField('nickname');//用户名称
            $list[$k]['nickname']=$user_name;
        }
        $twotime=date("Y-m-d H:i:s");
        $filename=$twotime.'用户资金信息.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $data .= "ID,";
        $data .= "用户名,";
        $data .= "操作金额,";
        $data .= "账户余额,";
        $data .= "操作类型,";
        $data .= "操作说明,";
        $data .= "创建时间,\n";
        
        if(!empty($list)){
            foreach($list as $k=>$v){
                $data .= $v['id'].",";
                $data .= $v['nickname'].",";
                $data .= $v['money'].",";
                $data .= $v['balance'].",";
                $data .= $v['note'].",";
                switch ($v['type']) {
                    case 1:
                        $type  = '充值';
                        break;
                    case 2:
                        $type  = '提现';
                        break;
                    case 3:
                        $type  = '下单';
                        break;
                    case 4:
                        $type  = '平仓';
                        break;
                    case 5:
                        $type  = '盈利';
                        break;
                    case 6:
                        $type  = '佣金';
                        break;
                    case 7:
                        $type  = '系统操作';
                        break;
                    case 8:
                        $type  = '提现返回';
                        break;
                    default:
                        $type  = '';
                        break;
                }
                $data .= $type.",";           
                $data .=date('Y-m-d H:i:s',$v['create_time'])."\t,\n";
            }
            echo  @mb_convert_encoding($data, 'GB2312','utf-8');
            exit;
        }
    }
}
